package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.WordUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BAnjuan;
import com.ruoyi.system.domain.BAnjuanScheme;
import com.ruoyi.system.service.IBAnjuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 查看案卷信息Controller
 * 
 * @author zzr
 * @date 2023-09-13
 */
@RestController
@RequestMapping("/system/shiti_anjuan")
public class BAnjuanController extends BaseController
{
    @Autowired
    private IBAnjuanService bAnjuanService;

    /**
     * 查询查看案卷信息列表
     */
//    //@PreAuthorize("@ss.hasPermi('system:anjuan:list')")
    @GetMapping("/list")
    public TableDataInfo list(BAnjuan bAnjuan)
    {
        startPage();
        // 添加排序条件，按照 "zongJuanNo" 字段排序
        PageHelper.orderBy("zongJuanNo");
        List<BAnjuan> list = bAnjuanService.selectBAnjuanList(bAnjuan);
        return getDataTable(list);
    }

    /**
     * 上报数据页面查询查看案卷信息列表
     */
    @GetMapping("/listReport")
    public TableDataInfo listReport(BAnjuan bAnjuan)
    {
        startPage();
        // 添加排序条件，按照 "zongJuanNo" 字段排序
        PageHelper.orderBy("zongJuanNo");
        List<BAnjuan> list = bAnjuanService.selectBAnjuanListReporter(bAnjuan);
        return getDataTable(list);
    }

    @GetMapping("/selectLastZongJuanNo")
    public Long selectLastZongJuanNo() {
        return bAnjuanService.selectLastZongJuanNo();
    }

    @GetMapping("/getMaxFeileiNo")
    public TableDataInfo getMaxFeileiNo(BAnjuan bAnjuan) {
        startPage();
        // 添加排序条件，按照 "anjuanno" 字段倒序排序
        PageHelper.orderBy("anjuanno desc");
        List<BAnjuan> list = bAnjuanService.selectBAnjuanList(bAnjuan);
        return getDataTable(list);
    }


    /**
     * 导出查看案卷信息列表
     */
//    //@PreAuthorize("@ss.hasPermi('system:anjuan:export')")
    @Log(title = "查看案卷信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BAnjuan bAnjuan)
    {
        List<BAnjuan> list = bAnjuanService.selectBAnjuanList(bAnjuan);
        ExcelUtil<BAnjuan> util = new ExcelUtil<BAnjuan>(BAnjuan.class);
        util.exportExcel(response, list, "查看案卷信息数据");
    }

    /**
     * 获取查看案卷信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:anjuan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bAnjuanService.selectBAnjuanById(id));
    }

    /**
     * 新增查看案卷信息
     */
    //@PreAuthorize("@ss.hasPermi('system:anjuan:add')")
    @Log(title = "查看案卷信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BAnjuan bAnjuan)
    {
        return toAjax(bAnjuanService.insertBAnjuan(bAnjuan));
    }

    /**
     * 修改查看案卷信息
     */
    //@PreAuthorize("@ss.hasPermi('system:anjuan:edit')")
    @Log(title = "查看案卷信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BAnjuan bAnjuan)
    {
        return toAjax(bAnjuanService.updateBAnjuan(bAnjuan));
    }

    /**
     * 删除查看案卷信息
     */
    //@PreAuthorize("@ss.hasPermi('system:anjuan:remove')")
    @Log(title = "查看案卷信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bAnjuanService.deleteBAnjuanByIds(ids));
    }

    /**
     * 生成总目录报表
     */
    @Log(title = "生成总目录报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSchemeZong")
    public void exportSchemeZong(HttpServletResponse resp, HttpServletRequest req, @RequestBody BAnjuan bAnjuan)
    {
        //获取列表
        List<BAnjuan> list = bAnjuanService.selectBAnjuanListReporter(bAnjuan);
        //按照总卷号排序
        Collections.sort(list, new Comparator<BAnjuan>() {
            @Override
            public int compare(BAnjuan o1, BAnjuan o2) {
                Long zongjuanNo1 = o1.getZongJuanNo();
                Long zongjuanNo2 = o2.getZongJuanNo();
                return zongjuanNo1.compareTo(zongjuanNo2);
            }
        });

        //生成结果集
        Map<String, Object> dataMap = new HashMap<String,Object>();
        ArrayList<BAnjuanScheme> resultList = new ArrayList<BAnjuanScheme>();

        for (BAnjuan s:list){
            if(s.getZongJuanNo() == null){
                s.setZongJuanNo(0L);
            }
            if(s.getFenLeiNo() == null || s.getFenLeiNo().length() == 0){
                s.setFenLeiNo(" ");
            }
            if(s.getJuanNo() == null){
                s.setJuanNo(0L);
            }
            if(s.getAnJuanName() == null || s.getAnJuanName().length() == 0){
                s.setAnJuanName(" ");
            }
            if(s.getLanTuZhangShu() == null || s.getLanTuZhangShu().length() == 0){
                s.setLanTuZhangShu(" ");
            }
            if(s.getDiTuZhangShu() == null || s.getDiTuZhangShu().length() == 0){
                s.setDiTuZhangShu(" ");
            }
            if(s.getWenZiCaiLiaoZhangShu() == null || s.getWenZiCaiLiaoZhangShu().length() == 0){
                s.setWenZiCaiLiaoZhangShu(" ");
            }
            if(s.getStorageLifeName() == null || s.getStorageLifeName().length() == 0){
                s.setStorageLifeName(" ");
            }
            if(s.getSecretLevelName() == null || s.getSecretLevelName().length() == 0){
                s.setSecretLevelName(" ");
            }

            if(s.getXingChengDanWei() == null || s.getXingChengDanWei().length() == 0){
                s.setXingChengDanWei(" ");
            }
            if(s.getKuNum() == null || s.getKuNum().length() == 0){
                s.setKuNum(" ");
            }
            if(s.getLeiNum() == null || s.getLeiNum().length() == 0){
                s.setLeiNum(" ");
            }
            if(s.getJiaNum() == null || s.getJiaNum().length() == 0){
                s.setJiaNum(" ");
            }
            if(s.getCengShu() == null || s.getCengShu().length() == 0){
                s.setCengShu(" ");
            }
            resultList.add(new BAnjuanScheme(s));
        }
        dataMap.put("schemeList",resultList);
        //设置生成文件存储路径
        String directory="/schemeList";
        WordUtil.CheckDownloadPath(directory);
        //生成文件
        WordUtil.CreateWord(resp,req,dataMap,
                "dataApparatusCheckScheme",
                "zongmulu.ftl",
                RuoYiConfig.getDownloadPath()+directory+"/zongmulu.doc");
        //下载后删除生成文件
        FileUtils.deleteFile(RuoYiConfig.getDownloadPath()+directory+"/zongmulu.doc");
    }


    /**
     * 生成分目录报表
     */
    @Log(title = "生成分目录报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSchemeFen")
    public void exportSchemeFen(HttpServletResponse resp, HttpServletRequest req, @RequestBody BAnjuan bAnjuan)
    {
        //获取列表
        List<BAnjuan> list = bAnjuanService.selectBAnjuanListReporter(bAnjuan);
        //按照总卷号排序
        Collections.sort(list, new Comparator<BAnjuan>() {
            @Override
            public int compare(BAnjuan o1, BAnjuan o2) {
                Long zongjuanNo1 = o1.getZongJuanNo();
                Long zongjuanNo2 = o2.getZongJuanNo();
                return zongjuanNo1.compareTo(zongjuanNo2);
            }
        });
        //生成结果集
        Map<String, Object> dataMap = new HashMap<String,Object>();
        ArrayList<BAnjuanScheme> resultList = new ArrayList<BAnjuanScheme>();

        for (BAnjuan s:list){
            if(s.getZongJuanNo() == null){
                s.setZongJuanNo(0L);
            }
            if(s.getFenLeiNo() == null || s.getFenLeiNo().length() == 0){
                s.setFenLeiNo(" ");
            }
            if(s.getJuanNo() == null){
                s.setJuanNo(0L);
            }
            if(s.getAnJuanName() == null || s.getAnJuanName().length() == 0){
                s.setAnJuanName(" ");
            }
            if(s.getLanTuZhangShu() == null || s.getLanTuZhangShu().length() == 0){
                s.setLanTuZhangShu(" ");
            }
            if(s.getDiTuZhangShu() == null || s.getDiTuZhangShu().length() == 0){
                s.setDiTuZhangShu(" ");
            }
            if(s.getWenZiCaiLiaoZhangShu() == null || s.getWenZiCaiLiaoZhangShu().length() == 0){
                s.setWenZiCaiLiaoZhangShu(" ");
            }
            if(s.getStorageLifeName() == null || s.getStorageLifeName().length() == 0){
                s.setStorageLifeName(" ");
            }
            if(s.getSecretLevelName() == null || s.getSecretLevelName().length() == 0){
                s.setSecretLevelName(" ");
            }

            if(s.getXingChengDanWei() == null || s.getXingChengDanWei().length() == 0){
                s.setXingChengDanWei(" ");
            }
            if(s.getKuNum() == null || s.getKuNum().length() == 0){
                s.setKuNum(" ");
            }
            if(s.getLeiNum() == null || s.getLeiNum().length() == 0){
                s.setLeiNum(" ");
            }
            if(s.getJiaNum() == null || s.getJiaNum().length() == 0){
                s.setJiaNum(" ");
            }
            if(s.getCengShu() == null || s.getCengShu().length() == 0){
                s.setCengShu(" ");
            }
            resultList.add(new BAnjuanScheme(s));
        }
        dataMap.put("schemeList",resultList);
        //设置生成文件存储路径
        String directory="/schemeList";
        WordUtil.CheckDownloadPath(directory);
        //生成文件
        WordUtil.CreateWord(resp,req,dataMap,
                "dataApparatusCheckScheme",
                "fenleimulu.ftl",
                RuoYiConfig.getDownloadPath()+directory+"/fenleimulu.doc");
        //下载后删除生成文件
        FileUtils.deleteFile(RuoYiConfig.getDownloadPath()+directory+"/fenleimulu.doc");
    }
}
